package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.CompanyDaoImpl;
import com.ysd.entity.Company;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminCompanyServlet extends BaseServlet {
	/**
	 * 查询公司表的所有数据 
	 * localhost:8080/meizizi/adminCompany?reqName=selCompany
	 */
	public void selCompany(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		CompanyDaoImpl cDao = new CompanyDaoImpl();
		Company com = cDao.getCompany();

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();// 创建out对象

		Gson gson = new Gson();// 创建gson对象

		String res = gson.toJson(com);// 将com转化为json对象
		out.print(res);// 响应

		out.flush();
		out.close();
	}

	/**
	 * localhost:8080/meizizi/adminCompany?reqName=updateCompanyById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateCompanyById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();// 创建out对象

		CompanyDaoImpl comDao = new CompanyDaoImpl();

		String name = request.getParameter("name");
		String webSite = request.getParameter("webSite");
		String introduction = request.getParameter("introduction");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		String address = request.getParameter("address");
		String culture = request.getParameter("culture");
		String logo = request.getParameter("logo");	

		Company com = new Company(name, webSite, introduction, phone, email, address, culture, logo);
		int n = comDao.updateCompany(com);

		if (n > 0) {
			out.print(Result.toClient(true, "修改成功", ""));
		} else {
			out.print(Result.toClient(false, "修改失败", ""));
		}

		out.flush();
		out.close();
	}
}
